<?php

/**
 * This is the model class for table "posts".
 * @property int $filter_word_id
 * @property int $user_id
 * @property int $time_created 
 * @property string $time_updated 
 */
class FilterWord extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{filter_word}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	return array(
	    array('word', 'required'),
	    array('filter_word_id, word, time_created, time_updated, user_id', 'safe')
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'word' => 'Word'
	);
    }

    public function beforeSave() {
	parent::beforeSave();
	if ($this->isNewRecord) {
	    $this->time_created = date('Y-m-d', time());
	}
	return true;
    }

    /*
     * 
     */

    public function afterSave() {
	parent::afterSave();
    }

    public function afterDelete() {
	parent::afterDelete();
    }

    /*
     * get list filter word
     */

    public static function getListFilter() {

	$aFilterWord = Yii::app()->cache->get('aFilterWord');
	if ($aFilterWord === false) {
	    //get list word
	    $listWord = db()->createCommand('SELECT * FROM {{filter_word}}')->queryAll();
	    $aFilterWord = array();
	    foreach ($listWord as $word) {
		$aFilterWord[] = $word['word'];
	    }
	    Yii::app()->cache->set('aFilterWord', $aFilterWord);
	}
	return $aFilterWord;
    }

    public function search() {
	$criteria = new CDbCriteria();
	$criteria->compare('word', $this->word, true);
	$criteria->order = 'filter_word_id DESC';

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    )
	));
    }

}

